package com.wjj.app;

import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.infrastructure.utils.ToastUtils;
import com.wjj.app.fragment.MainFragment1;
import com.wjj.app.fragment.MainFragment2;
import com.wjj.app.fragment.MainFragment3;

import java.util.ArrayList;

/**
 * Created by Wjj on 16/12/8.
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout linearLayout;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;

    //对fragment管理
    private FragmentManager fragmentManager;

    private MainFragment1 fragment1;
    private MainFragment2 fragment2;
    private MainFragment3 fragment3;

    private TextView testTv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        fragmentManager = getSupportFragmentManager();
//
//        linearLayout = (LinearLayout) findViewById(R.id.menu_layout);
//        tv1 = (TextView) linearLayout.findViewById(R.id.main_tv1);
//        tv2 = (TextView) linearLayout.findViewById(R.id.main_tv2);
//        tv3 = (TextView) linearLayout.findViewById(R.id.main_tv3);
//
//        tv1.setOnClickListener(this);
//        tv2.setOnClickListener(this);
//        tv3.setOnClickListener(this);
//
//        setTabSelection(1);

//        showPP();


        testTv = (TextView) findViewById(R.id.test_tv);


        ArrayList lists = new ArrayList();
        lists.add("wen");
        lists.add("jjj");
        lists.add("sjj");
        lists.add("uuu");
        lists.add(2, "111");

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < lists.size(); i++) {
            sb.append(lists.get(i));
        }

        testTv.setText(sb);
    }

    private void setTabSelection(int i) {
        clearSelection();

        // 开启一个Fragment事务
        android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);

//        switch (i) {
//            case 1:
//                tv1.setSelected(true);
//                if (fragment1 == null) {
//                    fragment1 = new MainFragment1();
//                    transaction.add(R.id.main_fragment_content, fragment1);
//                } else {
//                    transaction.show(fragment1);
//                }
//                break;
//            case 2:
//                tv2.setSelected(true);
//                if (fragment2 == null) {
//                    fragment2 = new MainFragment2();
//                    transaction.add(R.id.main_fragment_content, fragment2);
//                } else {
//                    transaction.show(fragment2);
//                }
//                break;
//            case 3:
//                tv3.setSelected(true);
//                if (fragment3 == null) {
//                    fragment3 = new MainFragment3();
//                    transaction.add(R.id.main_fragment_content, fragment3);
//                } else {
//                    transaction.show(fragment3);
//                }
//                break;
//        }
        transaction.commit();
    }

    private void hideFragments(android.support.v4.app.FragmentTransaction transaction) {
        if (fragment1 != null) {
            transaction.hide(fragment1);
        }
        if (fragment2 != null) {
            transaction.hide(fragment2);
        }
        if (fragment3 != null) {
            transaction.hide(fragment3);
        }
    }

    private void clearSelection() {
        tv1.setSelected(false);
        tv2.setSelected(false);
        tv3.setSelected(false);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_tv1:
                setTabSelection(1);
                break;
            case R.id.main_tv2:
                setTabSelection(2);
                break;
            case R.id.main_tv3:
                setTabSelection(3);
                break;
        }
    }

    private void showPP() {
        final TextView textView = new TextView(MainActivity.this);
        textView.setBackgroundColor(Color.BLACK);
        textView.setText("test    sdfsada");
        textView.setTextSize(20);
        textView.setTextColor(Color.WHITE);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_TOAST);//TYPE_TOAST 是关键  这样就不需要悬浮窗权限了
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;//禁止获得焦点，不然下面的界面无法接收到触摸点击事件  无法接收onBack
        params.width = 400;
        params.height = 200;
        params.format = PixelFormat.TRANSLUCENT;
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtils.showShortToast(MainActivity.this, "悬浮小球,我不要取现的");
            }
        });
        WindowManager windowManager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
        windowManager.addView(textView, params);
    }


}
